package com.yzcrm.erp.eqxiu.controller.mobile;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.HttpKit;
import com.jfinal.plugin.activerecord.Db;
import com.yzcrm.common.config.paragetter.BasePageRequest;
import com.yzcrm.common.constant.BaseConstant;
import com.yzcrm.erp.admin.entity.AdminApiLog;
import com.yzcrm.erp.eqxiu.service.EqxiuVisitService;
import com.yzcrm.swagger.annotation.ActionApi;
import com.yzcrm.swagger.annotation.Api;
import com.yzcrm.swagger.annotation.Param;
import com.yzcrm.swagger.annotation.Params;
import com.yzcrm.utils.BaseUtil;
import com.yzcrm.utils.R;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jiang.xue
 * 易企秀
 */
@Clear
@Api(remark = "易企秀手机端")
public class EqxiuVisitController extends Controller{

    @Inject
    private EqxiuVisitService eqxiuVisitService;

    /**
     * 访问
     */
    @ActionApi(summary = "访问", remark="访问")
    @Params({
            @Param(name="eventId", remark="事件Id", dataType="String", defaultValue="", required=true),
            @Param(name="ip", remark="iP地址", dataType="String", defaultValue="", required=true),
            @Param(name="address", remark="辽宁 大连 移动", dataType="String", defaultValue="", required=true),
            @Param(name="browser", remark="获取浏览器所有信息", dataType="String", defaultValue="", required=true),
            @Param(name="spreadUserId", remark="推广用户ID", dataType="String", defaultValue="", required=true),
    })
    public void visit(BasePageRequest basePageRequest) {

        renderJson(eqxiuVisitService.addVisit(basePageRequest.getJsonObject()));
    }


    /**
     * 注册
     */
    @ActionApi(summary = "注册", remark="注册")
    @Params({
            @Param(name="eventId", remark="事件Id", dataType="String", defaultValue="", required=true),
            @Param(name="name", remark="企业名称", dataType="String", defaultValue="", required=true),
            @Param(name="companyInfo", remark="天眼查结果列表选择的企业信息JSON串", dataType="String", defaultValue="", required=true),
            @Param(name="advanceAmount", remark="预采购金额", dataType="String", defaultValue="", required=true),
            @Param(name="receivables", remark="应收帐款总额", dataType="String", defaultValue="", required=true),
            @Param(name="address", remark="地址", dataType="String", defaultValue="", required=true),
            @Param(name="address_detail", remark="详细地址", dataType="String", defaultValue="", required=false),
            @Param(name="contact", remark="联系人", dataType="String", defaultValue="", required=true),
            @Param(name="phone", remark="联系电话", dataType="String", defaultValue="", required=true),
            @Param(name="spreadUserId", remark="推广用户ID", dataType="String", defaultValue="", required=true)
    })
    public void apply(BasePageRequest basePageRequest) {

        renderJson(eqxiuVisitService.addRegister(basePageRequest.getJsonObject()));
    }

    /**
     * 天眼查
     */
    @ActionApi(summary = "天眼查", remark="天眼查")
    @Params({
            @Param(name="name", remark="企业名称", dataType="String", defaultValue="", required=true)
    })
    public void queryCompanyInfoByName(@Para("name") String name) {

        if(ObjectUtil.isEmpty(name)) {
            renderJson(R.error("查询内容无效"));
            return;
        }
        StringBuffer url = new StringBuffer();
        String tianYanChaHttp = Db.queryStr("select value from yzcrm_admin_config where name = 'TianYanChaHttp'");
        String tianYanChaToken = Db.queryStr("select value from yzcrm_admin_config where name = 'TianYanChaToken'");
        Map<String, String> data = new HashMap<String, String>();
        data.put("word",name);
        url.append(tianYanChaHttp);
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", tianYanChaToken);
        String json = HttpKit.get(url.toString(),data,headers);
        JSONObject jsonObject = new JSONObject(json);
        if(jsonObject.getObj("result") != null) {
//        	记录天眼查的搜索记录
            AdminApiLog adminApiLog = new AdminApiLog();
            adminApiLog.setCreateTime(DateUtil.date());
            adminApiLog.setUserId(null);
            adminApiLog.setValue(name);
            adminApiLog.setType("TianYanCha");
            adminApiLog.save();
        }
        renderJson(R.ok().put("data",jsonObject));
    }

}
